package com.h2cloud;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.concurrent.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: wulongkai
 * @create: 2023-01-03 15:36
 **/

@Slf4j
public class lc20230103 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        String targetString = "<p>【请审批】员工<a style=\"color: #1f6dea; pointer-events: none; text-decoration-line: none;\" data-mark=\"【验证】姓名\">#1370079679251275776#</a>(OA：<a style=\"color: #1f6dea; pointer-events: none; text-decoration-line: none;\" data-mark=\"【验证】员工OA\">#1370577298763345921#</a>)的试用期转正申请</p>\t";
//        Set<String> fieldsFromTemplateContent = getFieldsFromTemplateContent(targetString);
//        System.out.println(fieldsFromTemplateContent);

//        Entity entity1 = new Entity(1, "11");
//        Entity entity2 = new Entity(2, "22");
//        Entity entity3 = new Entity(3, "33");
//        Entity entity4 = new Entity(4, "44");
//
//        List<Entity> list = new ArrayList<>();
//        list.add(entity1);
//        list.add(entity2);
//        list.add(entity3);
//        list.add(entity4);
//
//        Map<Integer, Entity> collect = list.stream().collect(Collectors.toMap(Entity::getId, Function.identity(), (key1, key2) -> key2));
//        System.out.println("collect = " + collect);

//        List<String> list2 = new ArrayList<>(Arrays.asList("ab", "cd", "ef"));
//        Map<String, String> collect = list2.stream().collect(Collectors.toMap(String::toUpperCase, Function.identity(), (key1, key2) -> key2));
//        System.out.println("collect = " + collect);
//
//        List<User> list = Arrays.asList(
//                new User().setId("1").setName("张小"),
//                new User().setId("2").setName("李大"),
//                new User().setId("2").setName("小明")
//        );
//        //2个参数的toMap方法
//        Map<String, String> map = list.stream()
//                .collect(Collectors.toMap(User::getId, User::getName, (v1, v2) -> v1));
//        System.out.println(map);
        String targetString = "<style>.ehrTemp{font-size:10.5pt;line-height:1.6;} .ehrTemp *{margin: 0;} .ehrTemp .default-link{text-decoration:none;color:#1f6dea;}</style><div class=\"ehrTemp\">\n" +
                "<table id=\"tinymceTempTable\" style=\"border-collapse: collapse; width: 720px;\">\n" +
                "<tbody>\n" +
                "<tr>\n" +
                "<td style=\"width: 100%;\">\n" +
                "<div id=\"tinymceTempTop\">\n" +
                "<div style=\"width: 100%; height: 133px; background-image: url('https://c.58cdn.com.cn/epfe/static/ehr/template_top.jpeg'); background-repeat: no-repeat; background-size: 100% 100%;\">&nbsp;</div>\n" +
                "</div>\n" +
                "<p style=\"line-height: 1.25;\"><span style=\"color: #000000; font-size: 10.5pt; font-family: 'Microsoft YaHei', 'Helvetica Neue';\">您好，</span></p>\n" +
                "<p style=\"line-height: 1.25;\">&nbsp;</p>\n" +
                "<p style=\"line-height: 1.25;\"><span style=\"color: #000000; font-size: 10.5pt; font-family: 'Microsoft YaHei', 'Helvetica Neue';\">员工<a class=\"ehrTempHighlight\" style=\"color: #000; pointer-events: none; text-decoration: none;\">#1370079679251275776#</a><a style=\"color: #000000; pointer-events: none;\" data-mark=\"【验证】姓名\">(</a>OA：<a class=\"ehrTempHighlight\" style=\"color: #000; pointer-events: none; text-decoration: none;\">#1370577298763345921#</a><a style=\"color: #000000; pointer-events: none;\" data-mark=\"【验证】员工OA\">)</a>的转正已审批通过，请您知悉，谢谢~</span></p>\n" +
                "<p style=\"line-height: 1.25;\">&nbsp;</p>\n" +
                "<p style=\"line-height: 1.25;\"><span style=\"color: #000000; font-family: 'Microsoft YaHei', 'Helvetica Neue'; font-size: 10.5pt;\">本邮件为系统自动发送，请不要回复。</span></p>\n" +
                "<p style=\"line-height: 1.60;\">&nbsp;</p>\n" +
                "<table id=\"tinymceTempTips\" style=\"border-collapse: collapse;\">\n" +
                "<tbody>\n" +
                "<tr>\n" +
                "<td style=\"width: 100%;\" href=\"baidu\"><br /><br />\n" +
                "<div style=\"border: 2px dashed #bbb; background: #eee; padding: 16px;\">\n" +
                "<h4>备注：</h4>\n" +
                "<br />\n" +
                "<p>以上所有数据均来源于H+系统，H+系统是58同城人力资源部&amp;TEG联合推出的基于SaaS模式的全新自研数智化人力资源系统，作为全集团管理者工作上的好帮手，58同城人力资源部一直致力于为管理者提供更准确的数据，更优质的服务。</p>\n" +
                "<br />\n" +
                "<p>欲了解更多详情，请点击：<a class=\"default-link\" href=\"https://ehr.58corp.com\">H+系统登录地址</a></p>\n" +
                "</div>\n" +
                "</td>\n" +
                "</tr>\n" +
                "</tbody>\n" +
                "</table>\n" +
                "<br />\n" +
                "<div id=\"tinymceTempBottom\">\n" +
                "<div style=\"width: 200px; height: 42px; background-image: url('https://c.58cdn.com.cn/epfe/static/ehr/template_bottom.jpeg'); background-repeat: no-repeat; background-size: 100% 100%;\">&nbsp;</div>\n" +
                "</div>\n" +
                "</td>\n" +
                "</tr>\n" +
                "</tbody>\n" +
                "</table>\n" +
                "</div>";
//        getFieldsFromTemplateContent(targetString);


//        String regex = "(.*)(\\d+)(.*)";
//        String input = "This is a sample Text, 1234, with numbers in between.";
//        //Creating a pattern object
//        Pattern pattern = Pattern.compile(regex);
//        //Matching the compiled pattern in the String
//        Matcher matcher = pattern.matcher(input);
//        if (matcher.find()) {
//            System.out.println("match: " + matcher.group(0));
//            System.out.println("First group match: " + matcher.group(1));
//            System.out.println("Second group match: " + matcher.group(2));
//            System.out.println("Third group match: " + matcher.group(3));
//        }

        String html =
                "<div class=\"ehrTemp\">\n" +
                        "<table style=\"border-collapse: collapse; width: 100%; height: 44px;\" border=\"1\">\n" +
                        "<tbody>\n" +
                        "<tr class=\"loop\">\n" +
                        "<td><a>#员工姓名#</a></td>\n" +
                        "<td><a>#员工生日#</a></td>\n" +
                        "</tr>\n" +
                        "</tbody>\n" +
                        "</table>\n" +
                        "</div>";
//        getHtml(html, "tr");
//        Document document = Jsoup.parse(html);
//        document.outputSettings(new Document.OutputSettings().prettyPrint(false));
//        Elements select = document.select(".loop");
//        System.out.println(html.replaceAll(select.get(0).toString(), " hrlllr "));

//        Elements trs = document.getElementsByTag("tr");
//        for (Element tr : trs) {
//            if (tr.toString().contains("background-color: #585858;")) {
//                System.out.println(tr);
//                String replace = tr.toString().replace("background-color: #585858;", "");
//                System.out.println(replace);
//                System.out.println();
//                System.out.println();
//            }
//        }

//        Map<Integer, List<Integer>> map = new HashMap<>();
//        map.putIfAbsent(1, new ArrayList<>());
//        map.get(1).add(11);
//        map.putIfAbsent(1, new ArrayList<>());
//        map.get(1).add(22);
//
//        System.out.println(map);

//        EmployeeWorkFlowSalaryChange p1 = new EmployeeWorkFlowSalaryChange();
//        test(p1);
//
//        HireEmployeeWorkFlowInfo p2 = new HireEmployeeWorkFlowInfo();
//        test(p2);

//        List<Entity> list = new ArrayList<>();
//        Entity e1 = new Entity(1, "1");
//        Entity e2 = new Entity(2, "2");
//        Entity e3 = new Entity(3, "3");
//        Entity e4 = new Entity(4, "4");
//        list.add(e1);
//        list.add(e2);
//        list.add(e3);
//        list.add(e4);
//
//        list.stream().forEach(o -> o.setId(22));
//        System.out.println("list = " + list);

        long l = System.currentTimeMillis();
        ExecutorService service = Executors.newCachedThreadPool();//默认是非公平锁
        List<Future<Map<String, String>>> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Callable callable = () -> sendMsg();
            Future submit = service.submit(callable);
            list.add(submit);
        }

        for (int i = 0; i < list.size(); i++) {
            Map<String, String> stringStringMap = list.get(i).get();
            System.out.println(stringStringMap);
        }
        System.out.println(System.currentTimeMillis() - l);
    }

    public static Map<String, String> sendMsg() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Map<String, String> map = new HashMap<>();
        map.put("1", "hello-" + new Random(1000).nextInt(1000));
        return map;
    }

    public static void test(AbstractBusinessObject u) {
        System.out.println(u.getClass().isAssignableFrom(EmployeeWorkFlowSalaryChange.class));
    }

    public static void getHtml2(String targetString, String tag, String classValue) {
        String startHtml = "<" + tag + "(.*?)class=\"" + classValue + "\"";
        String endHtml = "</" + tag + ">";
        String reg = startHtml + "(([\\s\\S])*?)" + endHtml; //插入的标签符号
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(targetString);
        while (matcher.find()) {
            if (matcher.group(1) == null) {
                continue;
            }
            System.out.println(matcher.group(1));
            System.out.println("===============");
        }

    }


    public static void getHtml(String targetString, String label) {
        getHtml(targetString, label, "loop=\"true\"");
    }

    public static void getHtml(String targetString, String tag, String loopLabel) {
        String startHtml = "<" + tag + " " + loopLabel;
        String endHtml = "</" + tag + ">";
        String reg = startHtml + "(([\\s\\S])*?)" + endHtml; //插入的标签符号
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(targetString);
        while (matcher.find()) {
            if (matcher.group(1) == null) {
                continue;
            }
            System.out.println(startHtml + matcher.group(1) + endHtml);
        }

    }

    public static void getFieldsFromTemplateContent(String targetString) {
        String reg = ">#([^#]*)#</a>|L#([^#]*)#R"; //插入的标签符号
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(targetString);
        while (matcher.find()) {
            String field = "";
            if (matcher.group(1) == null && matcher.group(2) == null) {
                continue;
            }
            System.out.println("matcher.group(1) = " + matcher.group(1));
            System.out.println("matcher.group(2) = " + matcher.group(2));
            System.out.println();
            field = matcher.group(1) != null ? matcher.group(1) : matcher.group(2);
            while (field.contains("&nbsp;") || field.startsWith(" ") || field.endsWith(" ")) {
                field = field.replaceAll("&nbsp;", "");
                field = field.trim();
            }
            log.info("模版业务模块info：field:{}", field);

            try {
                targetString = targetString.replace(">#" + field + "#</a>", ">" + "value.toString()" + "</a>");
                targetString = targetString.replace("L#" + field + "#R", "value.toString()");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

@Data
@AllArgsConstructor
class Entity {
    int id;
    String name;
}

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
class AbstractBusinessObject {
    String id;
    String name;
}


@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
class AbstractNoEffectiveEmployeeInfo extends AbstractBusinessObject {
    String id;
    String name;
}

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
class EmployeeWorkFlowSalaryChange extends AbstractNoEffectiveEmployeeInfo {
    String id;
    String name;
}

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
class HireEmployeeWorkFlowInfo extends AbstractBusinessObject {
    String id;
    String name;
}
